﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.KhuyenMaiHangHoa
{
    public partial class frmQuanLy : Form // VƯƠNG TRÍ LỢI
    {
        
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Khởi tạo

        bool checkClose = false;
        public Entities.ChinhSachGiaHangHoa[] hienthi;
        private Entities.ChinhSachGiaHangHoa chinhsachgiahanghoa;
        public Entities.ChinhSachGiaHangHoa temp;
        DateTime date;

        #endregion

        #region Delegate
        public void SelectData(string hanhdong)
        {
            try
            {
                dgvHienThi.DataSource = HienThi(date);
                hienthi = (Entities.ChinhSachGiaHangHoa[])dgvHienThi.DataSource;
                if (hanhdong == Common.Constants.Insert)
                {
                    frmXuLy XuLyCSGHH = new frmXuLy(Common.Constants.Insert);
                    XuLyCSGHH.Select = new frmXuLy.SelectData(SelectData);
                    XuLyCSGHH.Show();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region form quản lý chính sách hàng hóa

        public frmQuanLy()
        {
            InitializeComponent();
            try
            {
                date = Common.StaticVariable.InterfaceObject.SelectDate();

                // Hiển  thị listview
                dgvHienThi.DataSource = HienThi(date);
                hienthi = (Entities.ChinhSachGiaHangHoa[])dgvHienThi.DataSource;
                HienThiList();
                Event();
            }
            catch { }
        }


        // When enter on datagridview
        private void Event()
        {
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            rdbtimkiem.Checked = true;
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                hienthi = (Entities.ChinhSachGiaHangHoa[])dgvHienThi.DataSource;
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaChinhSachGiaHangHoa == dgvHienThi.CurrentRow.Cells["MaChinhSachGiaHangHoa"].Value.ToString())
                    {
                        chinhsachgiahanghoa = hienthi[i];
                        this.Sua();
                    }
                }
            }
        }


        void frmChinhSachGiaHangHoa_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
                else
                {
                }
            }
            catch
            {
            }
        }
       
        public Entities.ChinhSachGiaHangHoa[] HienThi(DateTime datetime)
        {
            try
            {
                Entities.ChinhSachGiaHangHoa[] chinhsachgiahang =Common.StaticVariable.InterfaceObject.SelectChinhSachGiaHangHoa();
               
                Entities.ChinhSachGiaHangHoa[] temp = new Entities.ChinhSachGiaHangHoa[chinhsachgiahang.Length];
                int sl = 0;
                for (int i = 0; i < chinhsachgiahang.Length; i++)
                {
                    if (chinhsachgiahang[i].Deleted == false)
                    {
                        temp[sl] = chinhsachgiahang[i];
                        sl++;
                    }
                }
                chinhsachgiahang = new Entities.ChinhSachGiaHangHoa[sl];
                for (int i = 0; i < chinhsachgiahang.Length; i++)
                {
                    chinhsachgiahang[i] = temp[i];
                }

                foreach (Entities.ChinhSachGiaHangHoa item in chinhsachgiahang)
                {
                    if (datetime.Date > item.NgayKetThuc.Date)
                        item.GiaTriTrangThai = "Đã sử dụng";
                    else if (datetime.Date < item.NgayBatDau.Date)
                        item.GiaTriTrangThai = "Sẽ sử dụng";
                    else if (datetime.Date >= item.NgayBatDau.Date && datetime.Date <= item.NgayKetThuc.Date)
                        item.GiaTriTrangThai = "Đang sử dụng";

                }

               // lblTongbanghi.Content = Common.Constants.TongSo + chinhsachgiahang.Length;
                return chinhsachgiahang;

            }
            catch 
            {
                return new Entities.ChinhSachGiaHangHoa[0];
                
            }
        }

        private void HienThiList()
        {
            try
            {
                if (hienthi == null)
                {
                    Entities.ChinhSachGiaHangHoa gitri = new Entities.ChinhSachGiaHangHoa();
                    dgvHienThi.DataSource = gitri;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaChinhSachGiaHangHoa"].HeaderText = "Chính Sách Giá Hàng Hóa";
                dgvHienThi.Columns["MaChinhSachGiaHangHoa"].Visible = true;
                dgvHienThi.Columns["NgayBatDau"].HeaderText = "Ngày Bắt Đầu";
                dgvHienThi.Columns["NgayBatDau"].Visible = true;
                dgvHienThi.Columns["NgayKetThuc"].HeaderText = "Ngày Kết Thúc";
                dgvHienThi.Columns["NgayKetThuc"].Visible = true;
                dgvHienThi.Columns["GiaTriTrangThai"].HeaderText = "Trạng Thái";
                dgvHienThi.Columns["GiaTriTrangThai"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            }
            catch
            { }
        }
        #endregion

        #region Sự kiện của window

        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {
                this.Closing += new System.ComponentModel.CancelEventHandler(frmChinhSachGiaHangHoa_Closing);
                this.WindowState = FormWindowState.Maximized;
                //Select firts row
                if (dgvHienThi.RowCount != 0)
                { 
                    chinhsachgiahanghoa = hienthi[0];
                }

                // click chuột trái image
                btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnThem.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnXem.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnXoa.MouseDown += new MouseEventHandler(MouseDown_Chung);
           
                // text changed
                txttimkiem.TextChanged += new EventHandler(txttimkiem_TextChanged);

               

                //
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
                
            }
            catch 
            {
            }

        }

        private void frmQuanLy_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.F2:
                        {
                            Them();
                            break;
                        }
                    case Keys.Delete:
                        {
                             Xoa();
                            break;
                        }
                    case Keys.Escape:
                        {
                            Thoat();
                            break;
                        }
                    case Keys.Enter:
                        {
                            Sua();
                            break;
                        }
                    default: break;
                }
            }
            catch 
            {    
            }
        }

        // Event SelectChange of listview
        public void dgvHienThi_CellClick(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaChinhSachGiaHangHoa == dgvHienThi.CurrentRow.Cells["MaChinhSachGiaHangHoa"].Value.ToString())
                    {
                        chinhsachgiahanghoa = hienthi[i];
                    }
                }
            }
            catch { }
        }

        //lvphieuchi_MouseDoubleClick
        public void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Sua();
            }
            catch { }

        }

        // Search data in database following display on listview
        public void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int soluong = 0;
                if (rdbtimkiem.Checked == true)
                {
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        int kiemtra = hienthi[i].MaChinhSachGiaHangHoa.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                        if (kiemtra >= 0)
                        {
                            soluong++;
                        }
                    }
                    if (soluong == 0)
                    {
                        dgvHienThi.DataSource = new Entities.ChinhSachGiaHangHoa[0];
                       //  lblTongbanghi.Content = Common.Constants.TongSo + "0";
                        return;
                    }
                    Entities.ChinhSachGiaHangHoa[] hienthitheoid = new Entities.ChinhSachGiaHangHoa[soluong];
                    soluong = 0;
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        int kiemtra = hienthi[i].MaChinhSachGiaHangHoa.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                        if (kiemtra >= 0)
                        {
                            hienthitheoid[soluong] = hienthi[i];
                            soluong++;
                        }
                    }
                    dgvHienThi.DataSource = hienthitheoid;
                    //lblTongbanghi.Content = Common.Constants.TongSo + hienthitheoid.Length;


                }
            }
            catch 
            {
            }
        
        }

        #endregion

        #region Check Permission

        public bool CheckPer(int hanhDong)
        {
           return Common.Utilities.Check(Common.FormName.ChinhSachGiaKM, hanhDong);
        }
       
        #endregion

        #region Nghiệp vụ
        // thêm
        public void Them()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check loop
              
                    frmXuLy XuLyCSGHH = new frmXuLy(Common.Constants.Insert);
                    XuLyCSGHH.Select = new frmXuLy.SelectData(SelectData);
                    XuLyCSGHH.Show();
                
            }
            catch
            {
            }
        }

        // Xem
        public void Sua()
        {
            try
            {
                // check per
                if (!CheckPer(0))
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaChinhSachGiaHangHoa == dgvHienThi.CurrentRow.Cells["MaChinhSachGiaHangHoa"].Value.ToString())
                    {
                        chinhsachgiahanghoa = hienthi[i];
                    }
                }
                // check object
                if (chinhsachgiahanghoa == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                if (this.hienthi.Length > 0)
                {
                    if (chinhsachgiahanghoa.MaChinhSachGiaHangHoa != null)
                    {
                        frmXuLy XuLyCSGHH = new frmXuLy(chinhsachgiahanghoa, hienthi, Common.Constants.Update);
                        XuLyCSGHH.Select = new frmXuLy.SelectData(SelectData);
                        XuLyCSGHH.Show();
                    }
                    else
                    { MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe); return; }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }


            }
            catch {}
        }

        //Xóa
        public void Xoa()
        {
            try
            {
                // check per
                if (!CheckPer(3))
                {
                    MessageBox.Show(Common.MSG.MSG11(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (chinhsachgiahanghoa == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
                          
                // check conflic delete
                if (!CheckConflicDelete())
                {
                    MessageBox.Show(Common.MSG.MSG3(chinhsachgiahanghoa.MaChinhSachGiaHangHoa), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                //check delete
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaChinhSachGiaHangHoa == dgvHienThi.CurrentRow.Cells["MaChinhSachGiaHangHoa"].Value.ToString())
                    {
                        chinhsachgiahanghoa = hienthi[i];
                    }
                }

                Entities.ChinhSachGiaHangHoa csghh = new Entities.ChinhSachGiaHangHoa();
                csghh.HanhDong = "Delete";
                csghh.ChinhSachGiaHHID = chinhsachgiahanghoa.ChinhSachGiaHHID;
                csghh.GiaTriTrangThai = chinhsachgiahanghoa.GiaTriTrangThai;
                if (csghh.GiaTriTrangThai == "Sẽ sử dụng")
                {
                    if (MessageBox.Show(Common.MSG.MSG1(chinhsachgiahanghoa.MaChinhSachGiaHangHoa), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        bool b = Common.StaticVariable.InterfaceObject.DeleteChinhSachGiaHangHoa(csghh);
                        if (b == false)
                        {
                            MessageBox.Show(csghh.ChinhSachGiaHHID.ToString());
                        }
                        else
                        {
                            Entities.ChiTietChinhSachGiaHangHoa ch = new Entities.ChiTietChinhSachGiaHangHoa();
                            ch.HanhDong = "Delete";
                            ch.MaChinhSachGiaHangHoa = chinhsachgiahanghoa.MaChinhSachGiaHangHoa;
                            bool xoachitiet = Common.StaticVariable.InterfaceObject.DeleteChiTietChinhSachGiaHangHoa(ch);
                            if (xoachitiet == false)
                            {
                            }
                            else { }
                            dgvHienThi.DataSource = HienThi(date);
                        }
                        hienthi = (Entities.ChinhSachGiaHangHoa[])dgvHienThi.DataSource;
                        for (int i = 0; i < hienthi.Length; i++)
                        {
                            if (hienthi[i].MaChinhSachGiaHangHoa == dgvHienThi.CurrentRow.Cells["MaChinhSachGiaHangHoa"].Value.ToString())
                            {
                                chinhsachgiahanghoa = hienthi[i];
                            }
                        }
                    }
                    else { return; }
                }
                else
                {
                    if (MessageBox.Show(csghh.MaChinhSachGiaHangHoa + " đã có giao dịch không thể xóa", Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        return;
                    }
                }

            }
            catch
            { }
        }

        public bool CheckConflicDelete()
        {
            try
            {
                Entities.ChinhSachGiaHangHoa[] temp = HienThi(date);
                for (int i = 0; i < temp.Length; i++)
                {
                    if (chinhsachgiahanghoa.MaChinhSachGiaHangHoa == temp[i].MaChinhSachGiaHangHoa)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        // Thoát
        public void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch { }
          
        }

        #endregion

        #region Sự kiện mouse down chung

        public void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;
                switch (temp.Name)
                {
                    case Common.Constants.btnThem:
                        {
                            Them();
                            break;
                        }
                    case Common.Constants.btnXem:
                        {
                            Sua();
                            break;
                        }
                    case Common.Constants.btnXoa:
                        {
                            Xoa(); 
                            break;
                        }
                    case Common.Constants.btnThoat:
                        {
                            Thoat();
                            break;
                        }
                    default: break;
                }
            }
            catch 
            {
                
                
            }
        
        }

        #endregion
     
    }
}
